package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.enums.SaleOrPurchType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.numGene.NumberGene;
import com.ruoyi.system.domain.AccountDetailss;
import com.ruoyi.system.domain.Funds;
import com.ruoyi.system.service.IAccountDetailssService;
import com.ruoyi.system.service.IFundsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CollectionsMapper;
import com.ruoyi.system.domain.Collections;
import com.ruoyi.system.service.ICollectionsService;

/**
 * 收退款Service业务层处理
 * 
 * @author he
 * @date 2025-10-20
 */
@Service
public class CollectionsServiceImpl implements ICollectionsService 
{
    @Autowired
    private CollectionsMapper collectionsMapper;
    @Autowired
    private IFundsService fundsService;
    @Autowired
    private IAccountDetailssService accountDetailssService;

    /**
     * 查询收退款
     * 
     * @param id 收退款主键
     * @return 收退款
     */
    @Override
    public Collections selectCollectionsById(Long id)
    {
        return collectionsMapper.selectCollectionsById(id);
    }

    /**
     * 查询收退款列表
     * 
     * @param collections 收退款
     * @return 收退款
     */
    @Override
    public List<Collections> selectCollectionsList(Collections collections)
    {
        return collectionsMapper.selectCollectionsList(collections);
    }

    /**
     * 新增收退款
     * 
     * @param collections 收退款
     * @return 结果
     */
    @Override
    public int insertCollections(Collections collections)
    {
        collections.setCreateTime(DateUtils.getNowDate());
        collections.setCreateUserId(SecurityUtils.getUserId());
        collections.setCreateUserName(SecurityUtils.getUsername());
        //查找账户

        if (collections.getSettlementType().equals(SaleOrPurchType.COLLECTION_TYPE.getCode().toString())){

            collections.setSettlementNumber(NumberGene.generateCode("SK"));

            //处理收款
            Funds funds = fundsService.selectFundsById(collections.getAccountId());

            BigDecimal totalCollection = funds.getTotalCollection();
            BigDecimal add = totalCollection.add(collections.getAmount());

            BigDecimal residueAmount = funds.getResidueAmount();
            BigDecimal residueAmountNew = residueAmount.add(collections.getAmount());

            Funds fundsUpd = new Funds();
            fundsUpd.setId(funds.getId());
            fundsUpd.setTotalCollection(add);
            fundsUpd.setResidueAmount(residueAmountNew);

            fundsService.updateFunds(fundsUpd);
            //账户详情
            AccountDetailss accountDetailss = new AccountDetailss();
            accountDetailss.setAmountId(funds.getId());
            accountDetailss.setTransactionNumber(collections.getSettlementNumber());
            accountDetailss.setTransactionAmountBefore(funds.getResidueAmount());
            accountDetailss.setTransactionAmount(collections.getAmount());
            accountDetailss.setTransactionType(SaleOrPurchType.COLLECTION_TYPE.getMessage());
            accountDetailss.setResidueAmount(residueAmountNew);
            accountDetailss.setAmountName(funds.getName());

            accountDetailssService.insertAccountDetailss(accountDetailss);
        } else if (collections.getSettlementType().equals(SaleOrPurchType.REFUSE_TYPE.getCode().toString())){
            collections.setSettlementNumber(NumberGene.generateCode("TK"));
            //处理退款
            Funds funds = fundsService.selectFundsById(collections.getAccountId());

            BigDecimal totalRefund = funds.getTotalRefund();
            BigDecimal subtract = totalRefund.subtract(collections.getAmount());

            BigDecimal residueAmount = funds.getResidueAmount();
            BigDecimal residueAmountNew = residueAmount.subtract(collections.getAmount());

            Funds fundsUpd = new Funds();
            fundsUpd.setId(funds.getId());
            fundsUpd.setTotalRefund(subtract);
            fundsUpd.setResidueAmount(residueAmountNew);

            fundsService.updateFunds(fundsUpd);

            //账户详情
            AccountDetailss accountDetailss = new AccountDetailss();
            accountDetailss.setAmountId(funds.getId());
            accountDetailss.setTransactionNumber(collections.getSettlementNumber());
            accountDetailss.setTransactionAmountBefore(funds.getResidueAmount());
            accountDetailss.setTransactionAmount(collections.getAmount());
            accountDetailss.setTransactionType(SaleOrPurchType.REFUSE_TYPE.getMessage());
            accountDetailss.setResidueAmount(residueAmountNew);
            accountDetailss.setAmountName(funds.getName());

            accountDetailssService.insertAccountDetailss(accountDetailss);
        }



        return collectionsMapper.insertCollections(collections);
    }

    /**
     * 修改收退款
     * 
     * @param collections 收退款
     * @return 结果
     */
    @Override
    public int updateCollections(Collections collections)
    {
        collections.setUpdateTime(DateUtils.getNowDate());
    collections.setUpdateUserId(SecurityUtils.getUserId());
    collections.setUpdateUserName(SecurityUtils.getUsername());
        return collectionsMapper.updateCollections(collections);
    }

    /**
     * 批量删除收退款
     * 
     * @param ids 需要删除的收退款主键
     * @return 结果
     */
    @Override
    public int deleteCollectionsByIds(Long[] ids)
    {
        return collectionsMapper.deleteCollectionsByIds(ids);
    }

    /**
     * 删除收退款信息
     * 
     * @param id 收退款主键
     * @return 结果
     */
    @Override
    public int deleteCollectionsById(Long id)
    {
        return collectionsMapper.deleteCollectionsById(id);
    }

    @Override
    public int updateApprovalState(List<Long> ids, Integer approvalState) {
        return collectionsMapper.updateApprovalState(ids, approvalState);
    }
}
